<?php

namespace app\common\command;

use mes\Mes;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;

/**
 * 物料进度--统计数据处理
 */
class Purchasing extends Command
{

    protected $logName = 'Purchasing';
    protected $mes = '';
    protected $isPrint = 1;    // 是否打印信息到终端

    protected $now;

    protected function configure()
    {
        $this->setName('Purchasing')
            ->addOption('type', 't', Option::VALUE_OPTIONAL, '分类', '')
            ->addOption('data', 'd', Option::VALUE_OPTIONAL, '分类', '')
            ->setDescription('物料进度--统计数据处理');
    }

    protected function execute(Input $input, Output $output)
    {
        $this->now = time();
        set_time_limit(60 * 60 * 5);
        ini_set('memory_limit', '512M');
        error_reporting(E_ALL & ~E_NOTICE);
        $type = $input->getOption('type');
        $data = $input->getOption('data');
        switch ($type) {
            case 'tb': //同步 statistics_purchasing表
                return $this->tongbu();
                break;
            case 'all':
                $this->all_run();
                break;
            case 'cg': //采购时效
                return $this->caigou();
                break;
            case 'fd': //
                return $this->finish_day_select_set();
                break;
            case 'fs': //
                return $this->fabric_shixiao();
                break;
            case 'set': //
                return $this->tiaoshu_status_set();
                break;
            case 'om_tb':
                return $this->material_shixiao_tongbu();
                break;
            case 'om':
                $this->material_shixiao_tongbu();
                $this->material_shixiao_checkcloth_shrink();
                $this->finish_day_select_set_material_shixiao();
                break;
            case 'temp':
                $this->ddd();
                break;
            case 'temp2':
                $this->temp2();
                break;
            case 'temp3':
                $this->update_caigou_status3();
                break;
            case 'my_return_first':
                $this->my_return_first();
                break;
            default:
                echo "Done" . PHP_EOL;
                break;
        }
    }

    private function all_run()
    {
        $param = $_POST;
        $lock = file_exists('/tmp') ? '/tmp/purchasing_all_run_pc_lock.txt' : env('runtime_path') . 'log/' . 'purchasing_all_run_pc_lock.txt';
        $fp = fopen($lock, 'a');
        if (flock($fp, LOCK_EX)) {
            //订单时效统计
            echo "订单时效统计" . PHP_EOL;
            $this->tongbu();
            $this->caigou();
            $this->fabric_shixiao();
            $this->finish_day_select_set();
            //面料时效统计
            echo "面料时效统计" . PHP_EOL;
            $this->material_shixiao_tongbu();
            $this->material_shixiao_checkcloth_shrink();
            $this->finish_day_select_set_material_shixiao();
            flock($fp, LOCK_UN);
        }
        fclose($fp);
        exit;
    }

    /**
     * statistics_purchasing表信息处理
     * 1,对应信息不一样，就同步
     *   1.1，正常订单，同步到statistics_purchasing表
     *   1.2，statistics_purchasing表有但，与order表信息不一致，就同步。
     * @author zhuguosen 905502314@qq.com
     * @date 2022/4/11
     */
    private function tongbu()
    {
        //1， 1.1，正常订单，同步到statistics_purchasing表
        for ($page = 1; $page < 1000000; $page++) {
            $where = [
                ['o.status', 'in', [0, 1, 2]],
                ['o.client_status', '<>', 99],
                ['o.stock_type', '<>', 10],
            ];
            $list = db('order o')
                ->field('o.id,o.client_ordernum,o.status,o.client_status,o.stock_type
                ,o.create_time,o.caigou_status,o.caigou_over_time,o.caichuang_status,o.caichuang_over_time
                ,sp.order_id')
                ->join('statistics_purchasing sp', "o.id=sp.order_id", 'left')
                ->where($where)
                ->whereRaw('sp.order_id is null')
                ->order('o.id asc')
                ->page($page)->limit(500)
                ->select();
            if (count($list) <= 0) {
                echo "1--结束了一共10*" . ($page - 1) . " 个数据" . PHP_EOL;
                return true;
            }
            $intsertdata = [];
            foreach ($list as $k => $v) {
                $intsertdata[] = [
                    'order_id' => $v['id'],
                    'client_ordernum' => $v['client_ordernum'],
                    'status' => $v['status'],
                    'client_status' => $v['client_status'],
                    'stock_type' => $v['stock_type'],
                    'order_create_time' => $v['create_time'],
                    'caigou_over_time' => $v['caigou_status'] == 1 ? intval($v['caigou_over_time']) : 0,
                    'caichuang_over_time' => $v['caichuang_status'] == 1 ? intval($v['caichuang_over_time']) : 0,

                    'update_time' => time(),
                    'create_time' => time(),
                ];
            }
            //10，statistics_purchasing表插入
            db('statistics_purchasing')->insertAll($intsertdata);
        }
        // 1.2，statistics_purchasing表有但，与order表信息不一致，就同步。
        for ($page = 1; $page < 1000000; $page++) {
            $list = db('statistics_purchasing sp')
                ->field('o.id,o.client_ordernum,o.status,o.client_status,o.stock_type
                ,o.create_time,o.caigou_status,o.caigou_over_time,o.caichuang_status,o.caichuang_over_time
                ,sp.order_id')
                ->join('order o', "sp.order_id=o.id", 'left')
                ->whereRaw("sp.status <> o.status 
                OR sp.client_status <> o.client_status 
                OR sp.stock_type <> o.stock_type 
                OR sp.caigou_over_time <> o.caigou_over_time 
                OR sp.caichuang_over_time <> o.caichuang_over_time 
                 ")
                ->page($page)->limit(10)
//                ->fetchSql(true)
                ->select();
            if (count($list) <= 0) {
                echo "2--结束了一共10*" . ($page - 1) . " 个数据" . PHP_EOL;
                return true;
            }
            foreach ($list as $k => $v) {
                //statistics_purchasing表修改
                db('statistics_purchasing')->where([
                    ['order_id', '=', $v['id']],
                ])->update([
                    'status' => $v['status'],
                    'client_status' => $v['client_status'],
                    'stock_type' => $v['stock_type'],
                    'caigou_over_time' => $v['caigou_status'] == 1 ? intval($v['caigou_over_time']) : 0,
                    'caichuang_over_time' => $v['caichuang_status'] == 1 ? intval($v['caichuang_over_time']) : 0,
//                    'caigou_over_time' => $v['caigou_over_time'],
//                    'caichuang_over_time' => $v['caichuang_over_time'],
                    'update_time' => time(),
                ]);
            }
        }
        echo "Done" . PHP_EOL;
        return false;
    }

    /**
     * 采购时效
     * 1,对应信息不一样，就同步
     *   1.1，正常订单，同步到statistics_purchasing表
     *   1.2，statistics_purchasing表有但，与order表信息不一致，就同步。
     * 2，查询statistics_purchasing表的 采购时效的数据，是否需要处理修改。
     * @author zhuguosen 905502314@qq.com
     * @date 2022/4/11
     */
    private function caigou()
    {
        $this->now = time();
        //1，查询 statistics_purchasing表没数据就插入 采购时效的数据
        for ($page = 1; $page < 1000000; $page++) {
            $where = [
//                ['sp.client_ordernum', '=', '8615099'],
                ['sp.caiguo_time_0', '=', 0],
                ['sp.update_time_0', '<', time() - 50],
                ['sp.status', 'in', [0, 1, 2]],
                ['sp.client_status', '<>', 99],
                ['sp.stock_type', '<>', 10],
                ['sp.flag_0', '=', 0],
            ];
            //1，合适单号查询
            $list = db('statistics_purchasing sp')
                ->field('sp.id,sp.order_id
                ')
//                ->join('order o', "sp.order_id=o.id", 'left')
//                ->join('order_material om', "sp.order_id=om.order_id", 'left')
//                ->join('material m', "om.material_id=m.id", 'left')
                ->where($where)
                ->page($page)->limit(100)
                ->select();
            if (count($list) <= 0) {
                echo "结束了一共10*" . ($page - 1) . " 个数据" . PHP_EOL;
                return true;
            }
            $order_id_array = [];
            foreach ($list as $k => $v) {
                $order_id_array[] = $v['order_id'];
            }
            //2,材料信息查询
            $list_order_material = db('order_material om')
                ->field("om.mid,om.material_id,om.order_id,om.client_ordernum
                ,om.is_caigou_start,om.caigou_start_time,om.caigou_finish_time
                ,m.name,m.type
                ")
                ->join('material m', "om.material_id=m.id", 'left')
                ->where([
                    ['om.order_id', 'in', $order_id_array],
                    ['om.material_id', '>', 0],
                    ['om.caigou_show', '<>', 0],// 去除不在采购进度显示条目
                ])
                ->group('om.mid')
                ->select();
            $update_edit = [];
            foreach ($list as $k => $v) {
                $list[$k]['omList'] = [];
                foreach ($list_order_material as $k2 => $sv) {
                    if ($v['order_id'] == $sv['order_id']) {
                        $list[$k]['omList'][] = $sv;
                    }
                }
                //caiguo_time_0   采购时间--所有料最未时间--已采购（辅料：拉链不计算在内）
                $list[$k]['caiguo_time_0'] = $this->_get_caiguo_time_0($list[$k]);
                $update_edit[$v['order_id']] = [
                    'caiguo_time_0' => $list[$k]['caiguo_time_0'],
                ];
            }
            $this->_update_order_material($update_edit, 'update_time_0');
            $this->_set_finish_day($update_edit);
        }
        echo "Done" . PHP_EOL;
        return false;
    }

    private function _update_order_material($update_edit, $update_time_type)
    {
        if (count($update_edit) <= 0) {
            return false;
        }
        $order_ids = [];
        $update_time_type_set = " set {$update_time_type} = " . $this->now . " ";
        $caiguo_time_0_set = ",caiguo_time_0 =( case";
        $caichuang_can_do_time_0_set = ",caichuang_can_do_time_0 =( case";
        $caichuang_can_do_time_1_set = ",caichuang_can_do_time_1 =( case";
        switch ($update_time_type) {
            case 'update_time_0':
                foreach ($update_edit as $k => $v) {
                    $order_ids[] = $k;
                    if (isset($v['caiguo_time_0']) && $v['caiguo_time_0'] > 0) {
                        $caiguo_time_0_set .= " when order_id={$k} then {$v['caiguo_time_0']} ";
                    }
                }
                break;
            case 'update_time_1':
                foreach ($update_edit as $k => $v) {
                    $order_ids[] = $k;
                    if (isset($v['caichuang_can_do_time_0']) && $v['caichuang_can_do_time_0'] > 0) {
                        $caichuang_can_do_time_0_set .= " when order_id={$k} then {$v['caichuang_can_do_time_0']} ";
                    }
                    if (isset($v['caichuang_can_do_time_1']) && $v['caichuang_can_do_time_1'] > 0) {
                        $caichuang_can_do_time_1_set .= " when order_id={$k} then {$v['caichuang_can_do_time_1']} ";
                    }
                }
                break;
        }
        switch ($update_time_type) {
            default:
                if ($caiguo_time_0_set == ",caiguo_time_0 =( case") {
                    $caiguo_time_0_set = '';
                } else {
                    $caiguo_time_0_set .= " end 
                ) ";
                }
                if ($caichuang_can_do_time_0_set == ",caichuang_can_do_time_0 =( case") {
                    $caichuang_can_do_time_0_set = '';
                } else {
                    $caichuang_can_do_time_0_set .= " end 
                ) ";
                }
                if ($caichuang_can_do_time_1_set == ",caichuang_can_do_time_1 =( case") {
                    $caichuang_can_do_time_1_set = '';
                } else {
                    $caichuang_can_do_time_1_set .= " end 
                ) ";
                }
                break;
        }
        $order_ids_str = implode(',', $order_ids);
        $sql = "update wx_statistics_purchasing 
                {$update_time_type_set}
                {$caiguo_time_0_set}
                {$caichuang_can_do_time_0_set}
                {$caichuang_can_do_time_1_set}
                where
                 order_id in ({$order_ids_str})
                ";
        $res = db()->execute($sql);
        return $res;
    }

    private function _get_caiguo_time_0($info)
    {
        //1，检测是否所有已采购
        $caigou_finish_time_array = [];
        foreach ($info['omList'] as $k => $v) {
            if ($v['type'] == 'accessories' && is_int(strpos($v['name'], '拉链'))) {
                continue;
            }
            if ($v['is_caigou_start'] < 2) {
                return 0;
            }
            $caigou_finish_time_array[] = intval($v['caigou_finish_time']);
        }
        if ($caigou_finish_time_array == []) {
            return 0;
        }
        //2，如是，得出最未时间
        return max($caigou_finish_time_array);
    }

    /**
     * 计算出finish_day的值
     * @author zhuguosen 905502314@qq.com
     * @date 2022/4/12
     */
    private function _set_finish_day($update_edit)
    {
        if (count($update_edit) <= 0) {
            return false;
        }
        $order_ids = [];
        foreach ($update_edit as $k => $v) {
            $order_ids[] = $k;
        }
        $list = db('statistics_purchasing sp')->where([
            ['sp.order_id', 'in', $order_ids],
        ])->select();
        $update_data = [];
        foreach ($list as $k => $v) {
            $update_data[$v['order_id']] = [];
            if ($v['order_create_time_0'] == 0) {
                $temp = date('H', $v['order_create_time']);
                if ($temp >= 18) {//下单当天18:00以后的订单，次日9时0分开始计算
                    $tomorrow_h9 = strtotime(date('Y-m-d 09:00:00', $v['order_create_time'])) + 24 * 3600;
                    $v['order_create_time_0'] = $update_data[$v['order_id']]['order_create_time_0'] = $tomorrow_h9;
                } else {
                    $v['order_create_time_0'] = $update_data[$v['order_id']]['order_create_time_0'] = $v['order_create_time'];
                }
            }
            if ($v['finish_day_0'] <= 0 && $v['caiguo_time_0'] > 0 && $v['order_create_time_0'] > 0) {
                $update_data[$v['order_id']]['finish_day_0'] = ($v['caiguo_time_0'] - $v['order_create_time_0']) / (24 * 3600);
            }
            if ($v['finish_day_0'] == 0 && $v['caiguo_time_0'] == 0 && $v['order_create_time_0'] > 0) {
                $update_data[$v['order_id']]['finish_day_0'] = -1;
            }
            if ($v['finish_day_1'] <= 0 && $v['caichuang_can_do_time_0'] > 0 && $v['caigou_over_time'] > 0) {
                $update_data[$v['order_id']]['finish_day_1'] = ($v['caichuang_can_do_time_0'] - $v['caigou_over_time']) / (24 * 3600);
            }
            if ($v['finish_day_1'] == 0 && $v['caichuang_can_do_time_0'] == 0 && $v['caigou_over_time'] > 0) {
                $update_data[$v['order_id']]['finish_day_1'] = -1;
            }
            if ($v['finish_day_2'] <= 0 && $v['caichuang_over_time'] > 0 && $v['caichuang_can_do_time_1'] > 0) {
                $update_data[$v['order_id']]['finish_day_2'] = ($v['caichuang_over_time'] - $v['caichuang_can_do_time_1']) / (24 * 3600);
            }
            if ($v['finish_day_2'] == 0 && $v['caichuang_over_time'] == 0 && $v['caichuang_can_do_time_1'] > 0) {
                $update_data[$v['order_id']]['finish_day_2'] = -1;
            }
            if ($v['finish_day_3'] <= 0 && $v['caichuang_over_time'] > 0 && $v['order_create_time_0'] > 0) {
                $update_data[$v['order_id']]['finish_day_3'] = ($v['caichuang_over_time'] - $v['order_create_time_0']) / (24 * 3600);
            }
            if ($v['finish_day_3'] == 0 && $v['caichuang_over_time'] == 0 && $v['order_create_time_0'] > 0) {
                $update_data[$v['order_id']]['finish_day_3'] = -1;
            }
        }
        //statistics_purchasing表修改
        $this->_update_finish_day_real($update_data);
    }

    private function _update_finish_day_real($update_data)
    {
        $order_ids = [];
        $order_create_time_0_set = ",order_create_time_0 =( case";
        $finish_day_0_set = ",finish_day_0 =( case";
        $finish_day_1_set = ",finish_day_1 =( case";
        $finish_day_2_set = ",finish_day_2 =( case";
        $finish_day_3_set = ",finish_day_3 =( case";
        foreach ($update_data as $k => $v) {
            if ($v == []) {
                continue;
            }
            $order_ids[] = $k;
            if (isset($v['order_create_time_0'])) {
                $order_create_time_0_set .= " when order_id={$k} then {$v['order_create_time_0']} ";
            }
            if (isset($v['finish_day_0'])) {
                $finish_day_0_set .= " when order_id={$k} then {$v['finish_day_0']} ";
            }
            if (isset($v['finish_day_1'])) {
                $finish_day_1_set .= " when order_id={$k} then {$v['finish_day_1']} ";
            }
            if (isset($v['finish_day_2'])) {
                $finish_day_2_set .= " when order_id={$k} then {$v['finish_day_2']} ";
            }
            if (isset($v['finish_day_3'])) {
                $finish_day_3_set .= " when order_id={$k} then {$v['finish_day_3']} ";
            }
        }
        switch (1) {
            default:
                if ($order_create_time_0_set == ",order_create_time_0 =( case") {
                    $order_create_time_0_set = '';
                } else {
                    $order_create_time_0_set .= " end 
                ) ";
                }
                if ($finish_day_0_set == ",finish_day_0 =( case") {
                    $finish_day_0_set = '';
                } else {
                    $finish_day_0_set .= " end 
                ) ";
                }
                if ($finish_day_1_set == ",finish_day_1 =( case") {
                    $finish_day_1_set = '';
                } else {
                    $finish_day_1_set .= " end 
                ) ";
                }
                if ($finish_day_2_set == ",finish_day_2 =( case") {
                    $finish_day_2_set = '';
                } else {
                    $finish_day_2_set .= " end 
                ) ";
                }
                if ($finish_day_3_set == ",finish_day_3 =( case") {
                    $finish_day_3_set = '';
                } else {
                    $finish_day_3_set .= " end 
                ) ";
                }
                break;
        }
        if ($order_ids == []) {
            return false;
        }
        $order_ids_str = implode(',', $order_ids);
        $sql = "update wx_statistics_purchasing 
        set order_id=order_id
                {$order_create_time_0_set}
                {$finish_day_0_set}
                {$finish_day_1_set}
                {$finish_day_2_set}
                {$finish_day_3_set}
                where
                 order_id in ({$order_ids_str})
                ";
        $res = db()->execute($sql);
        return $res;
    }

    /**
     * 查询应计算finish_day，但没计算的就计算一下。
     * @author zhuguosen 905502314@qq.com
     * @date 2022/4/12
     */
    private function finish_day_select_set()
    {
        $this->now = time();
        for ($page = 1; $page < 1000000; $page++) {
            //1，合适单号查询
            $list = db('statistics_purchasing sp')
                ->field('sp.id,sp.order_id
                ')
                ->whereRaw("
                (sp.caiguo_time_0>0 and sp.finish_day_0<=0)
                OR (sp.caigou_over_time>0 and sp.caichuang_can_do_time_0>0 and  sp.finish_day_1<=0)
                OR (sp.caichuang_can_do_time_1>0 and sp.caichuang_over_time>0 and  sp.finish_day_2<=0)
               OR (sp.caichuang_over_time>0 and  sp.finish_day_3<=0)
                ")
                ->page($page)->limit(100)
                ->select();
            if (count($list) <= 0) {
                echo "a结束了一共100*" . ($page - 1) . " 个数据" . PHP_EOL;
                break;
            }
            $update_edit = [];
            foreach ($list as $k => $v) {
                $update_edit[$v['order_id']] = [];
            }
            $this->_set_finish_day($update_edit);
        }
        //标识未完成的订单
        for ($page = 1; $page < 1000000; $page++) {
            $list = db('statistics_purchasing sp')
                ->field('sp.id,sp.order_id
                ')
                ->whereRaw("
                (sp.caiguo_time_0=0 and sp.finish_day_0=0)
                OR (sp.caigou_over_time>0 and sp.caichuang_can_do_time_0=0 and  sp.finish_day_1=0)
                OR (sp.caichuang_can_do_time_1>0 and sp.caichuang_over_time=0 and  sp.finish_day_2=0)
               OR (sp.caichuang_over_time=0 and  sp.finish_day_3=0)
                ")
                ->page($page)->limit(100)
                ->select();
            if (count($list) <= 0) {
                echo "b结束了一共100*" . ($page - 1) . " 个数据" . PHP_EOL;
                break;
            }
            $update_edit = [];
            foreach ($list as $k => $v) {
                $update_edit[$v['order_id']] = [];
            }
            $this->_set_finish_day($update_edit);
        }
        echo "Done" . PHP_EOL;
        return false;
    }

    /**
     * 采购时效
     * 1,对应信息不一样，就同步
     *   1.1，正常订单，同步到statistics_purchasing表
     *   1.2，statistics_purchasing表有但，与order表信息不一致，就同步。
     * 2，查询statistics_purchasing表的 采购时效的数据，是否需要处理修改。
     * @author zhuguosen 905502314@qq.com
     * @date 2022/4/11
     */
    private function fabric_shixiao()
    {
        $this->now = time();
        //1，查询 statistics_purchasing表没数据就插入 采购时效的数据
        for ($page = 1; $page < 1000000; $page++) {
            $where = [
//                ['sp.client_ordernum', '=', '8616534'],
                ['sp.caichuang_can_do_time_0', '=', 0],
                ['sp.update_time_1', '<', time() - 50],
                ['sp.caigou_over_time', '>', 0],
                ['sp.status', 'in', [0, 1, 2]],
                ['sp.client_status', '<>', 99],
                ['sp.stock_type', '<>', 10],
                ['sp.flag_1', '=', 0],
            ];
            //1，合适单号查询
            $list = db('statistics_purchasing sp')
                ->field('sp.id,sp.order_id')
                ->where($where)
                ->page($page)->limit(100)
                ->select();
            if (count($list) <= 0) {
                echo "结束了一共100*" . ($page - 1) . " 个数据" . PHP_EOL;
                return true;
            }
            $order_id_array = [];
            foreach ($list as $k => $v) {
                $order_id_array[] = $v['order_id'];
            }
            //2,材料信息查询
            $list_order_material = db('order_material om')
                ->field("om.mid,om.material_id,om.order_id,om.client_ordernum
                ,om.caichuang_can_do,om.caichuang_can_do_time
                ,m.name,m.type
                ")
                ->join('material m', "om.material_id=m.id", 'left')
                ->where([
                    ['om.order_id', 'in', $order_id_array],
                    ['om.material_id', '>', 0],
                    ['om.caigou_show', '<>', 0],// 去除不在采购进度显示条目
                    ['m.type', '=', 'fabric'],
                ])
                ->group('om.mid')
                ->select();
            $update_edit = [];
            foreach ($list as $k => $v) {
                $list[$k]['omList'] = [];
                foreach ($list_order_material as $k2 => $sv) {
                    if ($v['order_id'] == $sv['order_id']) {
                        $list[$k]['omList'][] = $sv;
                    }
                }
                //caichuang_can_do_time_0   可裁剪时间--面料最未的时间
                $caichuang_can_do_time_0 = [];
                foreach ($list[$k]['omList'] as $k2 => $v2) {
                    if ($v2['caichuang_can_do'] == 0) {
                        $caichuang_can_do_time_0 = [];
                        break;
                    }
                    $caichuang_can_do_time_0[] = intval($v2['caichuang_can_do_time']);
                }
                if ($caichuang_can_do_time_0 == []) {
                    $update_edit[$v['order_id']] = [
                        'caichuang_can_do_time_0' => 0,
                        'caichuang_can_do_time_1' => 0,
                    ];
                } else {
                    $update_edit[$v['order_id']] = [
                        'caichuang_can_do_time_0' => max($caichuang_can_do_time_0),
                        'caichuang_can_do_time_1' => min($caichuang_can_do_time_0),
                    ];
                }
            }
            $this->_update_order_material($update_edit, 'update_time_1');
            $this->_set_finish_day($update_edit);
        }
        echo "Done" . PHP_EOL;
        return false;
    }

    /**
     * order_material表的tiaoshu_status数值设置--tiaoshu_detail  tiaoshu分晰下。
     * @author zhuguosen 905502314@qq.com
     * @date 2022/4/13
     */
    private function tiaoshu_status_set()
    {
        $this->now = time();
        for ($page = 1; $page < 1000000; $page++) {
            //1，合适单号查询
            $list = db('order_material om')
                ->field('om.mid,om.order_id,om.client_ordernum
                ,om.tiaoshu,om.tiaoshu_detail,om.tiaoshu_status')
                ->whereRaw(" 1=1
                and o.status in (0,1,2)
                and om.tiaoshu_status=0 and om.tiaoshu_detail<>'' 
                and   om.tiaoshu_detail like '%-%'
                ")
                ->join('order o', "om.order_id=o.id", 'left')
                ->page($page)->limit(100)
                ->select();
            if (count($list) <= 0) {
                echo "结束了一共100*" . ($page - 1) . " 个数据" . PHP_EOL;
                return true;
            }
            $update_edit = [];
            foreach ($list as $k => $v) {
                $temp = 0;
                $ss = '$temp' . " ={$v['tiaoshu_detail']} ;";
                eval($ss);
                if ($temp <= $v['tiaoshu']) {
                    if ($temp == 0) {
                        $update_edit[$v['mid']] = [
                            'tiaoshu_status' => 10,
                        ];
                        continue;
                    }
                    if ($temp > 0) {
                        $update_edit[$v['mid']] = [
                            'tiaoshu_status' => 11,
                        ];
                        continue;
                    }
                }
            }
            if ($update_edit != []) {
                foreach ($update_edit as $k => $v) {
                    db('order_material')->where([
                        ['mid', '=', $k],
                    ])->update($v);
                }
            }
        }
        echo "Done" . PHP_EOL;
        return false;
    }

    /**
     * 时效统计
     * @author zhuguosen 905502314@qq.com
     * @date 2022/4/17
     */
    private function material_shixiao()
    {

    }

    /**
     * 时效统计=>数据同步
     * @author zhuguosen 905502314@qq.com
     * @date 2022/4/17
     */
    private function material_shixiao_tongbu()
    {
        //1， 1.1，正常订单，同步到statistics_purchasing_material表
        for ($page = 1; $page < 1000000; $page++) {
            $where = [
//                ['o.client_ordernum', '=', '8679708'],
                ['o.status', 'in', [0, 1, 2]],
                ['o.client_status', '<>', 99],
                ['o.stock_type', '<>', 10],
            ];
            $list = db('order_material om')
                ->field('o.id,o.client_ordernum,o.status,o.client_status,o.stock_type
                ,o.create_time,o.caigou_status,o.caigou_over_time,o.caichuang_status,o.caichuang_over_time
                ,sp.mid
                ,om.mid,om.arrive_time,om.caigou_start_time,om.caigou_finish_time
                ,om.caigou_first_time,om.make_tag_time,om.tiaoshu,om.make_tag_time_first
                ,om.need_checkcloth,om.need_shrink,om.caigou_show
                ,m.type
                ')
                ->join('statistics_purchasing_material sp', "om.order_id=sp.order_id", 'left')
                ->join('order o', "o.id=om.order_id", 'left')
                ->join('material m', "om.material_id=m.id", 'left')
//                //验布信息
//                ->join('checkcloth_shrink cs1', "om.order_id=cs1.order_id and cs1.type=1 and cs1.over_time>0", 'left')
//                //缩水信息
//                ->join('checkcloth_shrink cs2', "om.order_id=cs2.order_id and cs2.type=2 and cs2.over_time>0", 'left')
                ->where($where)
                ->whereRaw('sp.mid is null')
                ->order('om.mid asc')
                ->group('om.mid')
                ->page($page)->limit(100)
                ->select();
            if (count($list) <= 0) {
                echo "1--结束了一共100*" . ($page - 1) . " 个数据" . PHP_EOL;
                return true;
            }

            $intsertdata = [];
            foreach ($list as $k => $v) {
                $intsertdata[] = [
                    'order_id' => $v['id'],
                    'client_ordernum' => $v['client_ordernum'],
                    'mid' => $v['mid'],
                    'status' => $v['status'],
                    'client_status' => $v['client_status'],
                    'stock_type' => $v['stock_type'],
                    'order_create_time' => $v['create_time'],
                    'caigou_over_time' => $v['caigou_status'] == 1 ? intval($v['caigou_over_time']) : 0,
                    'caichuang_over_time' => $v['caichuang_status'] == 1 ? intval($v['caichuang_over_time']) : 0,

                    'arrive_time' => $v['arrive_time'],
                    'caigou_start_time' => $v['caigou_start_time'],
                    'caigou_finish_time' => $v['caigou_finish_time'],
                    'caigou_first_time' => $v['caigou_first_time'],
                    'make_tag_time' => $v['make_tag_time'],
                    'make_tag_time_first' => $v['make_tag_time_first'],
                    'tiaoshu' => $v['tiaoshu'],
                    'need_checkcloth' => $v['need_checkcloth'],
                    'need_shrink' => $v['need_shrink'],
                    'type' => $v['type'],
                    'caigou_show' => $v['caigou_show'],
                    'update_time' => time(),
                    'create_time' => time(),
                ];
            }
            //10，statistics_purchasing表插入
            db('statistics_purchasing_material')->insertAll($intsertdata);
        }
        // 1.2，statistics_purchasing_material表有但，与order表信息不一致，就同步。
        for ($page = 1; $page < 1000000; $page++) {
            $list = db('statistics_purchasing_material sp')
                ->field('sp.order_id,sp.mid
                ,o.client_ordernum,o.status,o.client_status,o.stock_type
                ,om.mid,om.arrive_time,om.caigou_start_time,om.caigou_finish_time
                ,om.caigou_first_time,om.make_tag_time,om.tiaoshu,om.make_tag_time_first
                ,om.caigou_show
                ')
                ->join('order o', "sp.order_id=o.id", 'left')
                ->join('order_material om', "sp.mid=om.mid", 'left')
                ->whereRaw("sp.status <> o.status
                OR sp.client_status <> o.client_status
                OR sp.stock_type <> o.stock_type
                OR sp.need_checkcloth <> om.need_checkcloth
                 OR sp.need_shrink <> om.need_shrink
                 OR sp.tiaoshu <> om.tiaoshu
                 OR sp.arrive_time <> om.arrive_time
                 OR sp.caigou_start_time <> om.caigou_start_time
                 OR sp.caigou_finish_time <> om.caigou_finish_time
                 OR sp.caigou_first_time <> om.caigou_first_time
                 OR sp.make_tag_time <> om.make_tag_time
                  OR sp.make_tag_time_first <> om.make_tag_time_first
                  OR sp.caigou_show <> om.caigou_show
                 ")
                ->page($page)->limit(10)
                ->select();
            if (count($list) <= 0) {
                echo "2--结束了一共10*" . ($page - 1) . " 个数据" . PHP_EOL;
                return true;
            }
            foreach ($list as $k => $v) {
                //statistics_purchasing表修改
                db('statistics_purchasing_material')->where([
//                    ['order_id', '=', $v['id']],
                    ['mid', '=', $v['mid']],
                ])->update([
                    'status' => $v['status'],
                    'client_status' => $v['client_status'],
                    'stock_type' => $v['stock_type'],
                    'tiaoshu' => $v['tiaoshu'],
                    'arrive_time' => $v['arrive_time'],
                    'caigou_start_time' => $v['caigou_start_time'],
                    'caigou_finish_time' => $v['caigou_finish_time'],
                    'caigou_first_time' => $v['caigou_first_time'],
                    'make_tag_time' => $v['make_tag_time'],
                    'make_tag_time_first' => $v['make_tag_time_first'],
                    'need_checkcloth' => $v['need_checkcloth'],
                    'need_shrink' => $v['need_shrink'],
                    'caigou_show' => $v['caigou_show'],
                    'update_time' => time(),
                ]);
            }
        }
        echo "Done" . PHP_EOL;
        return false;
    }

    /**
     * 时效统计=>面里料 的验布缩水时间处理
     * @author zhuguosen 905502314@qq.com
     * @date 2022/4/17
     */
    private function material_shixiao_checkcloth_shrink()
    {
        for ($page = 1; $page < 1000000; $page++) {
            $where = [
//                ['sp.client_ordernum', '=', '8679708'],
                ['sp.status', 'in', [0, 1, 2]],
                ['sp.client_status', '<>', 99],
                ['sp.stock_type', '<>', 10],
                ['sp.type', '<>', 'accessories'],
                ['sp.update_time_3', '<', time() - 50 * 60],
            ];
            $list = db('statistics_purchasing_material sp')
                ->field('sp.order_id,sp.mid,sp.type,sp.tiaoshu
                ,sp.need_checkcloth,sp.need_shrink
                ,count(cs1.over_time) as cs1_over_count,cs1.over_time as cs1_over_time
                ,count(cs2.over_time) as cs2_over_count,cs2.over_time as cs2_over_time
                ')
//                ->join('statistics_purchasing_material sp', "om.order_id=sp.order_id", 'left')
//                ->join('order o', "o.id=om.order_id", 'left')
//                ->join('material m', "om.material_id=m.id", 'left')
                //验布信息
                ->join('checkcloth_shrink cs1', "sp.need_checkcloth=1 and sp.order_id=cs1.order_id and cs1.type=1 and cs1.is_return=0 and cs1.over_time>0", 'left')
                //缩水信息
                ->join('checkcloth_shrink cs2', "sp.need_shrink=1 and sp.order_id=cs2.order_id and cs2.type=2 and cs2.is_return=0 and cs2.over_time>0", 'left')
                ->where($where)
                ->whereRaw(" sp.need_checkcloth=1 OR sp.need_shrink=1")
                ->group('sp.order_id,sp.mid')
                ->page($page)->limit(100)
                ->select();
//            var_dump($list);die;
            if (count($list) <= 0) {
                echo "3--结束了一共10*" . ($page - 1) . " 个数据" . PHP_EOL;
                return true;
            }
            $update_edit = [];
            foreach ($list as $k => $v) {
                $temp = [
//                    'mid' => $v['mid'],
                    'order_id' => $v['order_id'],
                    'checkcloth_over_time' => 0,
                    'shrink_over_time' => 0,
                ];
                if ($v['tiaoshu'] <= 0) {
                    $update_edit[$v['mid']] = $temp;
                    continue;
                }
                if ($v['need_checkcloth'] == 1 && $v['tiaoshu'] == $v['cs1_over_count']) {
                    $temp['checkcloth_over_time'] = intval($v['cs1_over_time']);
                }
                if ($v['need_shrink'] == 1 && $v['tiaoshu'] == $v['cs2_over_count']) {
                    $temp['shrink_over_time'] = intval($v['cs2_over_time']);
                }
                $update_edit[$v['mid']] = $temp;
            }
            $this->_material_shixiao_update_edit($update_edit, 'update_time_3');
            $this->_set_finish_day_material_shixiao($update_edit);
        }
    }

    private function _material_shixiao_update_edit($update_edit, $update_time_type)
    {
        if (count($update_edit) <= 0) {
            return false;
        }
        $mids = [];
        $update_time_type_set = " set {$update_time_type} = " . $this->now . " ";
        $checkcloth_over_time_set = ",checkcloth_over_time =( case";
        $shrink_over_time_set = ",shrink_over_time =( case";
        switch ($update_time_type) {
            case 'update_time_3':
                foreach ($update_edit as $k => $v) {
                    $mids[] = $k;
                    if (isset($v['checkcloth_over_time'])) {
                        $checkcloth_over_time_set .= " when mid={$k}  then {$v['checkcloth_over_time']} ";
                    }
                    if (isset($v2['shrink_over_time'])) {
                        $shrink_over_time_set .= " when  mid={$k}  then {$v['shrink_over_time']} ";
                    }
                }
                break;
        }
        switch ($update_time_type) {
            default:
                if ($checkcloth_over_time_set == ",checkcloth_over_time =( case") {
                    $checkcloth_over_time_set = '';
                } else {
                    $checkcloth_over_time_set .= " end 
                ) ";
                }
                if ($shrink_over_time_set == ",shrink_over_time =( case") {
                    $shrink_over_time_set = '';
                } else {
                    $shrink_over_time_set .= " end 
                ) ";
                }
                break;
        }
        $mids_str = implode(',', $mids);
        $sql = "update wx_statistics_purchasing_material
                {$update_time_type_set}
                {$checkcloth_over_time_set}
                {$shrink_over_time_set}
                where
                 mid in ({$mids_str})
                ";
        $res = db()->execute($sql);
        return $res;
    }

    /**
     * 计算出finish_day的值
     * @author zhuguosen 905502314@qq.com
     * @date 2022/4/12
     */
    private function _set_finish_day_material_shixiao($update_edit)
    {
        if (count($update_edit) <= 0) {
            return false;
        }
        $mids = [];
        foreach ($update_edit as $k => $v) {
            $mids[] = $k;
        }
        $list = db('statistics_purchasing_material sp')->where([
            ['sp.mid', 'in', $mids],
        ])->select();
        $update_data = [];
        foreach ($list as $k => $v) {
            $update_data[$v['order_id']] = [];
            if ($v['arrive_time'] > 0 && $v['arrive_time_0'] == 0) {
                $temp = date('H', $v['arrive_time']);
                if ($temp >= 20) {//当天20:00以后，次日9时0分开始计算
                    $tomorrow_h9 = strtotime(date('Y-m-d 09:00:00', $v['arrive_time'])) + 24 * 3600;
                    $v['arrive_time_0'] = $update_data[$v['mid']]['arrive_time_0'] = $tomorrow_h9;
                } else {
                    $v['arrive_time_0'] = $update_data[$v['mid']]['arrive_time_0'] = $v['arrive_time'];
                }
            }
            if ($v['caigou_start_time'] > 0 && $v['caigou_start_time_0'] == 0) {
                $temp = date('H', $v['caigou_start_time']);
                if ($temp >= 20) {//当天20:00以后，次日9时0分开始计算
                    $tomorrow_h9 = strtotime(date('Y-m-d 09:00:00', $v['caigou_start_time'])) + 24 * 3600;
                    $v['caigou_start_time_0'] = $update_data[$v['mid']]['caigou_start_time_0'] = $tomorrow_h9;
                } else {
                    $v['caigou_start_time_0'] = $update_data[$v['mid']]['caigou_start_time_0'] = $v['caigou_start_time'];
                }
            }
            if ($v['finish_day_0'] <= 0 && $v['arrive_time_0'] > 0 && $v['caigou_finish_time'] > 0) {
                $update_data[$v['mid']]['finish_day_0'] = ($v['caigou_finish_time'] - $v['arrive_time_0']) / (3600);
                $update_data[$v['mid']]['finish_day_0'] < 0 && $update_data[$v['mid']]['finish_day_0'] = 0.02;
            }
            if ($v['finish_day_0'] <= 0 && $v['arrive_time_0'] > 0 && $v['caigou_finish_time'] == 0) {
                $update_data[$v['mid']]['finish_day_0'] = -1;
            }
            if ($v['finish_day_1'] <= 0 && $v['caigou_finish_time'] > 0) {
                if ($v['caigou_start_time_0'] > 0) {
                    $update_data[$v['mid']]['finish_day_1'] = ($v['caigou_finish_time'] - $v['caigou_start_time_0']) / (3600);
                    $update_data[$v['mid']]['finish_day_1'] <= 0 && $update_data[$v['mid']]['finish_day_1'] = 0.02;
                } else {
                    $update_data[$v['mid']]['finish_day_1'] = 0.01;
                }
            }
            if ($v['finish_day_1'] <= 0 && $v['caigou_finish_time'] == 0) {
                if ($v['caigou_start_time_0'] == 0) {
                    $update_data[$v['mid']]['finish_day_1'] = -1;
                }
            }
            if ($v['finish_day_2'] <= 0 && $v['arrive_time'] > 0 && $v['caigou_first_time'] > 0) {
                $update_data[$v['mid']]['finish_day_2'] = ($v['caigou_first_time'] - $v['arrive_time']) / (3600);
                $update_data[$v['mid']]['finish_day_2'] < 0 && $update_data[$v['mid']]['finish_day_2'] = 0.02;
            }
            if ($v['finish_day_2'] <= 0 && $v['arrive_time'] > 0 && $v['caigou_first_time'] == 0) {
                $update_data[$v['mid']]['finish_day_2'] = -1;
            }
            if ($v['finish_day_3'] <= 0 && $v['make_tag_time_first'] > 0 && $v['checkcloth_over_time'] > 0) {
                $update_data[$v['mid']]['finish_day_3'] = ($v['checkcloth_over_time'] - $v['make_tag_time']) / (3600);
            }
            if ($v['finish_day_3'] <= 0 && $v['make_tag_time_first'] > 0 && $v['checkcloth_over_time'] == 0) {
                $update_data[$v['mid']]['finish_day_3'] = -1;
            }
            if ($v['finish_day_4'] <= 0 && $v['make_tag_time_first'] > 0 && $v['shrink_over_time'] > 0) {
                if ($v['need_checkcloth'] == 1 && $v['checkcloth_over_time'] > 0) {
                    $update_data[$v['mid']]['finish_day_4'] = ($v['shrink_over_time'] - $v['checkcloth_over_time']) / (3600);
                }
                if ($v['need_checkcloth'] == 0) {
                    $update_data[$v['mid']]['finish_day_4'] = ($v['shrink_over_time'] - $v['make_tag_time_first']) / (3600);
                }
                $update_data[$v['mid']]['finish_day_4'] < 0 && $update_data[$v['mid']]['finish_day_4'] = 0.02;
            }
            if ($v['finish_day_4'] <= 0 && $v['make_tag_time_first'] > 0 && $v['shrink_over_time'] == 0) {
                if ($v['need_checkcloth'] == 1 && $v['checkcloth_over_time'] > 0) {
                    $update_data[$v['mid']]['finish_day_4'] = -1;
                }
                if ($v['need_checkcloth'] == 0) {
                    $update_data[$v['mid']]['finish_day_4'] = -1;
                }
            }
        }
        //statistics_purchasing表修改
        $this->_update_finish_day_real_material_shixiao($update_data);
    }

    private function _update_finish_day_real_material_shixiao($update_data)
    {
        $mids = [];
        $order_create_time_0_set = ",order_create_time_0 =( case";
        $finish_day_0_set = ",finish_day_0 =( case";
        $finish_day_1_set = ",finish_day_1 =( case";
        $finish_day_2_set = ",finish_day_2 =( case";
        $finish_day_3_set = ",finish_day_3 =( case";
        $finish_day_4_set = ",finish_day_4 =( case";
        foreach ($update_data as $k => $v) {
            if ($v == []) {
                continue;
            }
            $mids[] = $k;
            if (isset($v['finish_day_0'])) {
                $finish_day_0_set .= " when mid={$k} then {$v['finish_day_0']} ";
            }
            if (isset($v['finish_day_1'])) {
                $finish_day_1_set .= " when mid={$k} then {$v['finish_day_1']} ";
            }
            if (isset($v['finish_day_2'])) {
                $finish_day_2_set .= " when mid={$k} then {$v['finish_day_2']} ";
            }
            if (isset($v['finish_day_3'])) {
                $finish_day_3_set .= " when mid={$k} then {$v['finish_day_3']} ";
            }
            if (isset($v['finish_day_4'])) {
                $finish_day_4_set .= " when mid={$k} then {$v['finish_day_4']} ";
            }
        }
        switch (1) {
            default:
                if ($finish_day_0_set == ",finish_day_0 =( case") {
                    $finish_day_0_set = '';
                } else {
                    $finish_day_0_set .= " end 
                ) ";
                }
                if ($finish_day_1_set == ",finish_day_1 =( case") {
                    $finish_day_1_set = '';
                } else {
                    $finish_day_1_set .= " end 
                ) ";
                }
                if ($finish_day_2_set == ",finish_day_2 =( case") {
                    $finish_day_2_set = '';
                } else {
                    $finish_day_2_set .= " end 
                ) ";
                }
                if ($finish_day_3_set == ",finish_day_3 =( case") {
                    $finish_day_3_set = '';
                } else {
                    $finish_day_3_set .= " end 
                ) ";
                }
                if ($finish_day_4_set == ",finish_day_4 =( case") {
                    $finish_day_4_set = '';
                } else {
                    $finish_day_4_set .= " end 
                ) ";
                }
                break;
        }
        if ($mids == []) {
            return false;
        }
        $mids_str = implode(',', $mids);
        $sql = "update wx_statistics_purchasing_material 
        set order_id=order_id
                {$finish_day_0_set}
                {$finish_day_1_set}
                {$finish_day_2_set}
                {$finish_day_3_set}
                {$finish_day_4_set}
                where
                 mid in ({$mids_str})
                ";
        $res = db()->execute($sql);
        return $res;
    }

    private function error_order()
    {
        $this->now = time();

        for ($page = 1; $page < 1000000; $page++) {
            //1，合适单号查询
            $list = db('order_material om')
                ->field('om.mid,om.order_id,om.client_ordernum
                ,om.tiaoshu,om.tiaoshu_detail,om.tiaoshu_status')
                ->whereRaw(" 1=1
                and o.status in (0,1)
                and om.tiaoshu_status=0 and om.tiaoshu_detail<>'' 
                and   om.tiaoshu_detail like '%-%'
                ")
                ->join('order o', "om.order_id=o.id", 'left')
                ->page($page)->limit(10000)
                ->select();
            if (count($list) <= 0) {
                echo "结束了一共100*" . ($page - 1) . " 个数据" . PHP_EOL;
                return true;
            }
            $update_edit = [];
            foreach ($list as $k => $v) {
                $temp = 0;
                $ss = '$temp' . " ={$v['tiaoshu_detail']} ;";
                eval($ss);
                if ($temp != $v['tiaoshu']) {
                    $update_edit[] = $v['mid'];
                }
            }
            $update_edit_str = implode(',', $update_edit);
            addLog($update_edit_str, 'sam_test400');
        }
        echo "Done" . PHP_EOL;
        return false;
    }

    /**
     * 查询应计算finish_day，但没计算的就计算一下。--时效统计
     * @author zhuguosen 905502314@qq.com
     * @date 2022/4/18
     */
    private function finish_day_select_set_material_shixiao()
    {
        $this->now = time();
        for ($page = 1; $page < 1000000; $page++) {
            //1，合适单号查询
            $list = db('statistics_purchasing_material sp')
                ->field('sp.id,sp.mid
                ')
                ->whereRaw("
                (sp.arrive_time>0 and sp.caigou_finish_time>0 and sp.finish_day_0 <=0)
                OR ( sp.caigou_finish_time>0 and  sp.finish_day_1<=0)
                OR (sp.arrive_time>0 and sp.caigou_first_time>0 and  sp.finish_day_2<=0)
               OR (sp.make_tag_time_first>0 and sp.checkcloth_over_time>0  and  sp.finish_day_3<=0)
                OR (sp.need_checkcloth=0  and sp.make_tag_time_first>0 and sp.shrink_over_time>0 and  sp.finish_day_4<=0)
                OR (sp.need_checkcloth=1  and sp.checkcloth_over_time>0 and sp.shrink_over_time>0 and  sp.finish_day_4<=0)
                
                 OR (sp.arrive_time>0 and sp.arrive_time_0=0 )
                  OR (sp.caigou_start_time>0 and sp.caigou_start_time=0)
                ")
                ->page($page)->limit(100)
                ->select();
            if (count($list) <= 0) {
                echo "a结束了一共100*" . ($page - 1) . " 个数据" . PHP_EOL;
                break;
            }
            $update_edit = [];
            foreach ($list as $k => $v) {
                $update_edit[$v['mid']] = [];
            }
            $this->_set_finish_day_material_shixiao($update_edit);
        }
        //标识未完成的订单
        for ($page = 1; $page < 1000000; $page++) {
            //1，合适单号查询
            $list = db('statistics_purchasing_material sp')
                ->field('sp.id,sp.mid
                ')
                ->whereRaw("
                (sp.arrive_time>0 and sp.caigou_finish_time=0 and sp.finish_day_0=0)
                OR ( sp.caigou_start_time>0 and  sp.finish_day_1=0)
                OR (sp.arrive_time>0 and sp.caigou_first_time=0 and  sp.finish_day_2=0)
               OR (sp.make_tag_time_first>0 and sp.checkcloth_over_time=0  and  sp.finish_day_3=0)
                OR (sp.need_checkcloth=0  and sp.make_tag_time_first>0 and sp.shrink_over_time=0 and  sp.finish_day_4=0)
                OR (sp.need_checkcloth=1  and sp.checkcloth_over_time>0 and sp.shrink_over_time=0 and  sp.finish_day_4=0)
                
                ")
                ->page($page)->limit(100)
                ->select();
            if (count($list) <= 0) {
                echo "b结束了一共100*" . ($page - 1) . " 个数据" . PHP_EOL;
                break;
            }
            $update_edit = [];
            foreach ($list as $k => $v) {
                $update_edit[$v['mid']] = [];
            }
            $this->_set_finish_day_material_shixiao($update_edit);
        }
        echo "Done" . PHP_EOL;
        return false;
    }

    public function temp()
    {
        //1,查询关联信息，wx_order_material  wx_sku
        for ($page = 1; $page < 1000000; $page++) {
            $where = [
//                ['o.client_ordernum', '=', '8580370'],
                ['o.real_shipment_status', '=', 1],
            ];
            $list = db('order o')
                ->field(" o.id,o.client_ordernum
")
                ->join('wx_order_material om', 'o.id=om.order_id', 'left')
                ->join('wx_order_material_item omi', 'o.id=omi.order_id', 'left')
                ->where($where)
                ->whereRaw('om.mid is null and omi.id is not null ')
                ->page($page)->limit(50)->select();
            if (count($list) <= 0) {
                echo "结束了一共10*{$page} 个数据" . PHP_EOL;
                return true;
            }
            $insertAll_data = [];
            $order_ids = [];
            foreach ($list as $k => $v) {
                $order_ids[] = $v['id'];
            }
            $insertAll_data = db('order_material_19')->where([
                ['order_id', 'in', $order_ids],
            ])->select();
            foreach ($insertAll_data as $k => $v) {
                db()->startTrans();
                try {
                    db('order_material')->insert($v);
                    $info = db('order_material')->field('mid')->where([
                        ['mid', '=', $v['mid']],
                        ['order_id', '=', $v['order_id']],
                        ['material_id', '=', $v['material_id']],
                    ])->find();
                    if (!isset($info['mid'])) {
                        db()->rollback();
                        var_dump($info, $v['mid']);
                        return false;
                    }
                    db()->commit();
                } catch (\Exception $e) {
                    db()->rollback();
                    var_dump($v['mid']);
                    throw new \Exception($e->getMessage());
                    return false;
                }
            }
//            db('order_material_temp')->insertAll($insertAll_data);
//            db('order_material')->insertAll($insertAll_data);
        }
        //2,
    }

    public function temp2()
    {
        //1,查询关联信息，wx_order_material  wx_sku
        for ($page = 1; $page < 1000000; $page++) {
//            $where = [
//                ['om19.client_ordernum', '=', '8606023'],
//            ];
            $insertAll_data = $list = db('order_material_19 om19 ')
                ->field("om19.*
")
                ->join('order_material om', 'om19.mid=om.mid', 'left')
//                ->where($where)
                ->whereRaw(' om.mid is null and om19.mid is not null ')
                ->page($page)->limit(100)->select();
//            var_dump($insertAll_data);die;
            if (count($list) <= 0) {
                echo "结束了一共10*{$page} 个数据" . PHP_EOL;
                return true;
            }
//            $insertAll_data=[];
//            $order_ids=[];
//            foreach ($list as $k=>$v){
//                $order_ids[]=$v['id'];
//            }
//            $insertAll_data=db('order_material_19')->where([
//                ['order_id','in',$order_ids],
//            ])->select();
            foreach ($insertAll_data as $k => $v) {
                db()->startTrans();
                try {
                    db('order_material')->insert($v);
                    $info = db('order_material')->field('mid')->where([
                        ['mid', '=', $v['mid']],
                        ['order_id', '=', $v['order_id']],
                        ['material_id', '=', $v['material_id']],
                    ])->find();
                    if (!isset($info['mid'])) {
                        db()->rollback();
                        var_dump($info, $v['mid']);
                        return false;
                    }
                    db()->commit();
                } catch (\Exception $e) {
                    db()->rollback();
                    var_dump($v['mid']);
                    throw new \Exception($e->getMessage());
                    return false;
                }
            }
//            db('order_material_temp')->insertAll($insertAll_data);
//            db('order_material')->insertAll($insertAll_data);
        }
        //2,
    }

    public function temp20()
    {
        //1,查询关联信息，wx_order_material  wx_sku
        for ($page = 1; $page < 1000000; $page++) {
            $where = [
                ['om19.client_ordernum', '=', '8580370'],
            ];
            $insertAll_data = $list = db('order_material_19 om19 ')
                ->field("om19.*
")
                ->join('wx_order_material om', 'om19.order_id=om.order_id', 'left')
//                ->where($where)
                ->whereRaw('om.mid is null and om19.mid is not null ')
                ->page($page)->limit(1)->select();
//            var_dump($insertAll_data);die;
            if (count($list) <= 0) {
                echo "结束了一共10*{$page} 个数据" . PHP_EOL;
                return true;
            }
//            $insertAll_data=[];
//            $order_ids=[];
//            foreach ($list as $k=>$v){
//                $order_ids[]=$v['id'];
//            }
//            $insertAll_data=db('order_material_19')->where([
//                ['order_id','in',$order_ids],
//            ])->select();
            foreach ($insertAll_data as $k => $v) {
                db()->startTrans();
                try {
                    db('order_material')->insert($v);
                    $info = db('order_material')->field('mid')->where([
                        ['mid', '=', $v['mid']],
                        ['order_id', '=', $v['order_id']],
                        ['material_id', '=', $v['material_id']],
                    ])->find();
                    if (!isset($info['mid'])) {
                        db()->rollback();
                        var_dump($info, $v['mid']);
                        return false;
                    }
                    db()->commit();
                } catch (\Exception $e) {
                    db()->rollback();
                    var_dump($v['mid']);
                    throw new \Exception($e->getMessage());
                    return false;
                }
            }
//            db('order_material_temp')->insertAll($insertAll_data);
//            db('order_material')->insertAll($insertAll_data);
        }
        //2,
    }

    public function temp3()
    {
        //1,查询关联信息，wx_order_material  wx_sku
        for ($page = 1; $page < 1000000; $page++) {
            $where = [
//                ['o.client_ordernum', '=', '8580370'],
                ['o.real_shipment_status', '=', 1],
            ];
            $insertAll_data = $list = db('sku_material_19 sm19')
                ->field("sm19.*
")
                ->join('sku_material sm', 'sm19.mid=sm.mid', 'left')
//                ->where($where)
                ->whereRaw('sm.mid is null and sm19.mid is not null ')
                ->page($page)->limit(1)->select();
            if (count($list) <= 0) {
                echo "结束了一共10*{$page} 个数据" . PHP_EOL;
                return true;
            }
            foreach ($insertAll_data as $k => $v) {
                db()->startTrans();
                try {
                    db('sku_material')->insert($v);
                    $info = db('sku_material')->field('mid')->where([
                        ['mid', '=', $v['mid']],
                        ['sku_id', '=', $v['sku_id']],
                        ['material_id', '=', $v['material_id']],
                        ['create_time', '=', $v['create_time']],
                    ])->find();
                    if (!isset($info['mid'])) {
                        db()->rollback();
                        var_dump($info, $v['mid']);
                        return false;
                    }
                    db()->commit();
                } catch (\Exception $e) {
                    db()->rollback();
                    var_dump($v['mid']);
                    throw new \Exception($e->getMessage());
                    return false;
                }
            }
        }
    }

    public function ddd()
    {
        //1,查询关联信息，wx_order_material  wx_sku
        for ($page = 1; $page < 1000000; $page++) {
            // om.client_ordernum='8646858' and om.mid=278889 and
            $list = db('order_material om')
                ->field("
            om.mid,om.order_id,om.material_id,om.tiaoshu,om.tiaoshu_detail,om.tiaoshu_status,count(omi.id) as  count_id
            ,sum(if(omi.is_return=0,1,0)) as  count_id2
            ,om.arrive_time
            ")
                ->join('order_material_item omi', 'om.mid=omi.order_material_id and omi.flag_del=0', 'left')
                ->whereRaw("
        om.shiji_caigou_count >0 and omi.id is not null and om.create_time>1646064000")
                ->group('om.mid')
                ->page($page)->limit(1000)
                ->select();
            echo "结束了一共10*{$page} 个数据" . PHP_EOL;
            if (count($list) <= 0) {
                echo "结束了一共10*{$page} 个数据" . PHP_EOL;
                return true;
            }
//            var_dump($list);die;
            $dd = new \app\common\service\CheckclothShrink();
            foreach ($list as $k => $v) {
                if ($v['count_id'] > 0 && $v['count_id2'] == 0) {
                    db('order_material')->whereRaw("mid={$v['mid']}")->update([
                        'is_make_tag' => 1,
//                    'caigou_status'=>1,
                        'is_caigou_start' => 2,
                        'arrive_time' => $v['arrive_time'] > 0 ? $v['arrive_time'] : time(),
                        'shiji_caigou_count' => $v['count_id2'],
                        'tiaoshu' => $v['count_id2'],
                        'tiaoshu_detail' => '',
                        'tiaoshu_status' => 10,
                    ]);
                    $dd->is_over_checkcloth_shrink($v['order_id'], $v['material_id']);
                    continue;
                }
                if ($v['count_id'] != $v['count_id2'] && $v['count_id2'] > 0) {
                    $temp = 0;
                    $ss = '$temp' . " ={$v['tiaoshu_detail']} ;";
                    eval($ss);
                    db('order_material')->whereRaw("mid={$v['mid']}")->update([
                        'is_make_tag' => 1,
//                    'caigou_status'=>1,
                        'is_caigou_start' => 2,
                        'arrive_time' => $v['arrive_time'] > 0 ? $v['arrive_time'] : time(),
                        'shiji_caigou_count' => $v['count_id2'],
                        'tiaoshu' => $v['count_id2'],
                        'tiaoshu_detail' => $temp == $v['count_id2'] ? $v['tiaoshu_detail'] : $v['count_id2'],
                        'tiaoshu_status' => 11,
                    ]);
                    $dd->is_over_checkcloth_shrink($v['order_id'], $v['material_id']);
                }
            }
        }
    }

    public function ddd2()
    {
        //1,查询关联信息，wx_order_material  wx_sku
        for ($page = 1; $page < 1000000; $page++) {
            $list = db('order_material om')
                ->field("
            om.mid,om.order_id,om.material_id,om.tiaoshu,om.tiaoshu_status,count(omi.id) as  count_id
            ,sum(if(omi.is_return=0,1,0)) as  count_id2
            ,om.arrive_time
            ")
                ->join('order_material_item omi', 'om.mid=omi.order_material_id and omi.flag_del=0', 'left')
                ->whereRaw("om.tiaoshu_detail=0 and om.create_time>1646064000")
                ->group('om.mid')
                ->page($page)->limit(1000)
                ->select();
            echo "结束了一共10*{$page} 个数据" . PHP_EOL;
            if (count($list) <= 0) {
                echo "结束了一共10*{$page} 个数据" . PHP_EOL;
                return true;
            }
//            var_dump($list);die;
            $dd = new \app\common\service\CheckclothShrink();
            foreach ($list as $k => $v) {
                db('order_material')->whereRaw("mid={$v['mid']}")->update([
                    'tiaoshu_detail' => '',
                ]);
            }
        }
    }

    //有空才弄下条数明细的显示问题。不过新旧数据交叉，不准的。
    public function ddd3()
    {
        //1,查询关联信息，wx_order_material  wx_sku
        for ($page = 1; $page < 1000000; $page++) {
            $sql = "SELECT ct,type,tiaoshu,cloth_num,time FROM (
(SELECT 1 as ct,type,tiaoshu, cloth_num,create_time as time 
FROM wx_order_material_log 
WHERE order_id=97212 AND material_id=24542)

UNION All 
(SELECT 2 as ct,mrl.type,1 as tiaoshu,mr.cloth_num,mrl.create_time 
FROM wx_material_return_log mrl
LEFT JOIN wx_material_return mr ON mrl.material_return_id=mr.id
WHERE mr.order_id=97212 AND mr.material_id=24542)

			) tb ORDER BY time DESC LIMIT 0,20
			";
            $list = db('order_material om')
                ->field("
            om.mid,om.order_id,om.material_id,om.tiaoshu,om.tiaoshu_status,count(omi.id) as  count_id
            ,sum(if(omi.is_return=0,1,0)) as  count_id2
            ,om.arrive_time
            ")
                ->join('order_material_item omi', 'om.mid=omi.order_material_id and omi.flag_del=0', 'left')
                ->whereRaw("om.tiaoshu_detail=0 and om.create_time>1646064000")
                ->group('om.mid')
                ->page($page)->limit(1000)
                ->select();
            if (count($list) <= 0) {
                echo "结束了一共10*{$page} 个数据" . PHP_EOL;
                return true;
            }
//            var_dump($list);die;
            $dd = new \app\common\service\CheckclothShrink();
            foreach ($list as $k => $v) {
            }
        }
    }

    public function update_caigou_status()
    {
        // and o.client_ordernum=8419112  and o.caigou_status=0 and o.caigou_over_time>0
        $orderMaterial_serivr = new \app\common\service\OrderMaterial();
        define('ADMIN_ID', 0);
        //1,查询关联信息，wx_order_material  wx_sku
        for ($page = 1; $page < 1000000; $page++) {
            $list = db('order o')
                ->field(" o.id,o.client_ordernum
")
                ->whereRaw('1=1
                and o.status in (0,1)')
                ->page($page)->limit(50)->select();
            if (count($list) <= 0) {
                echo "结束了一共10*{$page} 个数据" . PHP_EOL;
                return true;
            }
            foreach ($list as $k => $v) {
                $orderMaterial_serivr->update_caigou_status([], 0, [$v['id']]);
            }
        }
    }

    public function update_caigou_status2()
    {
        // and o.client_ordernum=8419112  and o.caigou_status=0 and o.caigou_over_time>0
        $orderMaterial_serivr = new \app\common\service\OrderMaterial();
        define('ADMIN_ID', 0);
        //1,查询关联信息，wx_order_material  wx_sku
        for ($page = 1; $page < 1000000; $page++) {
            $list = db('order o')
                ->field(" o.id,o.client_ordernum
")
                ->whereRaw('1=1
                and o.status in (0,1) and o.caichuang_can_do=1')
                ->page($page)->limit(50)->select();
            if (count($list) <= 0) {
                echo "结束了一共10*{$page} 个数据" . PHP_EOL;
                return true;
            }
            foreach ($list as $k => $v) {
                $orderMaterial_serivr->update_caigou_status([], 0, [$v['id']]);
            }
        }
    }

    public function update_caigou_status3_old()
    {
        //1,查询关联信息，wx_order_material  wx_sku
        for ($page = 1; $page < 1000000; $page++) {
            $where = [
//                ['om19.client_ordernum', '=', '8580370'],
            ];
            $insertAll_data = $list = db('order_26 o26 ')
                ->field("o26.id,o26.client_ordernum, o26.caigou_status,o26.caigou_over_time
")
                ->join('order o', 'o.id=o26.id', 'left')
//                ->where($where)
                ->whereRaw('1=1
                and o.status in (0,1) and o.caichuang_can_do=1 and o26.caigou_over_time>0 and o.caigou_over_time=0')
                ->page($page)->limit(1)->select();
//            var_dump($insertAll_data);die;
            if (count($list) <= 0) {
                echo "结束了一共10*{$page} 个数据" . PHP_EOL;
                return true;
            }
            foreach ($insertAll_data as $k => $v) {
                db()->startTrans();
                try {
                    db('order')->where("id={$v['id']}")->update([
                        'caigou_status'=>$v['caigou_status'],
                        'caigou_over_time'=>$v['caigou_over_time'],
                    ]);
//                    $info = db('order_material')->field('mid')->where([
//                        ['mid', '=', $v['mid']],
//                        ['order_id', '=', $v['order_id']],
//                        ['material_id', '=', $v['material_id']],
//                    ])->find();
//                    if (!isset($info['mid'])) {
//                        db()->rollback();
//                        var_dump($info, $v['mid']);
//                        return false;
//                    }
                    db()->commit();
                } catch (\Exception $e) {
                    db()->rollback();
                    var_dump($v['mid']);
                    throw new \Exception($e->getMessage());
                    return false;
                }
            }
        }
        //2,
    }

    public function update_caigou_status3()
    {
        //1,查询关联信息，wx_order_material  wx_sku
        for ($page = 1; $page < 1000000; $page++) {
            $where = [
//                ['om19.client_ordernum', '=', '8580370'],
            ];
            $insertAll_data = $list = db('order_26 o26 ')
                ->field("o26.id,o26.client_ordernum, o26.caigou_status,o26.caigou_over_time,o26.caigou_over_time
")
                ->join('order o', 'o.id=o26.id', 'left')
//                ->where($where)
                ->whereRaw('1=1
                and o.status in (0,1) and o.caigou_over_time > o.caichuang_over_time and o.caichuang_over_time>0')
                ->page($page)->limit(1)->select();
            if (count($list) <= 0) {
                echo "结束了一共10*{$page} 个数据" . PHP_EOL;
                return true;
            }
            foreach ($insertAll_data as $k => $v) {
                db()->startTrans();
                try {
                    db('order')->where("id={$v['id']}")->update([
//                        'caigou_status'=>$v['caigou_status'],
                        'caigou_over_time'=>$v['caigou_over_time'],
                    ]);
//                    $info = db('order_material')->field('mid')->where([
//                        ['mid', '=', $v['mid']],
//                        ['order_id', '=', $v['order_id']],
//                        ['material_id', '=', $v['material_id']],
//                    ])->find();
//                    if (!isset($info['mid'])) {
//                        db()->rollback();
//                        var_dump($info, $v['mid']);
//                        return false;
//                    }
                    db()->commit();
                } catch (\Exception $e) {
                    db()->rollback();
                    var_dump($v['mid']);
                    throw new \Exception($e->getMessage());
                    return false;
                }
            }
        }
        //2,
    }


    public function update_caigou_status100()
    {
        // and o.client_ordernum=8419112  and o.caigou_status=0 and o.caigou_over_time>0
        $orderMaterial_serivr = new \app\common\service\OrderMaterial();
        //1,查询关联信息，wx_order_material  wx_sku
        for ($page = 1; $page < 1000000; $page++) {
            $list = db('order o')
                ->field(" o.id,o.client_ordernum
")
                ->whereRaw('1=1
                and o.status in (0,1) and o.caigou_over_time > o.caichuang_over_time')
//                ->page($page)->limit(50)
                ->count();
            var_dump($list);die;
            if (count($list) <= 0) {
                echo "结束了一共10*{$page} 个数据" . PHP_EOL;
                return true;
            }
            foreach ($list as $k => $v) {

            }
        }
    }

    /**
     * 返单第一次标识下 只运行一次
     * @author zhuguosen 905502314@qq.com
     * @date 2022/6/8
     */
    public function my_return_first()
    {
        $orderLogic=new \app\common\logic\Order();
        //1,查询关联信息，wx_order_material  wx_sku
        for ($page = 1; $page < 1000000; $page++) {
            $list = db('order o')->field('id,sku,create_time,count(o.id) as count')
                ->whereRaw(" type='new'  ")
                ->group('sku')
                ->page($page)->limit(100)->select();
            if (count($list) <= 0) {
                echo "结束了一共100*{$page} 个数据" . PHP_EOL;
                return true;
            }
            foreach ($list as $k => $v) {
                if ($v['count'] > 1) {
                    $temp = db('order o')->field('id,sku,create_time,count(o.id) as count')
                        ->whereRaw(" o.sku='{$v['sku']}' and type='new'  ")
                        ->order('create_time asc')
                        ->limit(1)
                        ->find();
                    $list[$k] = $temp;
                }
            }
            //处理
            foreach ($list as $k => $orderData) {
                $my_return_first_info = db('order')->field('id')->where([
                    'sku' => $orderData['sku'],
                    'type_ext' => 'my_return_first',
                ])->find();
                if (isset($my_return_first_info['id'])) {
                   continue;
                }
                $f_t=$orderData['create_time'] + 24 * 3600;
                $temp_info=db('order')->field('id')
                    ->whereRaw("sku='{$orderData['sku']}' and create_time>={$f_t}")
                    ->order('create_time asc')
                    ->limit(1)
                    ->find();
                if(isset($temp_info['id'])){
                    db('order')->whereRaw("id={$temp_info['id']}")->update([
                        'type_ext'=>'my_return_first',
                    ]);
                }
            }
        }
    }
}